﻿using System;
using System.Collections.Generic;
using System.IO;

namespace JEngine.Framework
{
    public class FileHelper
    {
    
        public void Save(string path, string info)
        {
            try
            {
                var dicPath = Path.GetDirectoryName(path);
                if (!Directory.Exists(dicPath))
                    Directory.CreateDirectory(dicPath);
                File.WriteAllText(path, info);
            }
            catch (Exception e)
            {
                this.LogError($"Save Error:{e} ");
            }
            
        }
        public void Save(string path, byte[] bytes)
        {
            try
            {
                var dicPath = Path.GetDirectoryName(path);
                if (!Directory.Exists(dicPath))
                    Directory.CreateDirectory(dicPath);
                File.WriteAllBytes(path, bytes);
            }
            catch (Exception e)
            {
               this.LogError($"Save Error:{e} ");
            }
            
        }

        public void GetAllFiles(List<string> files, string dir)
        {
            try
            {
                string[] fls = Directory.GetFiles(dir);
                foreach (string fl in fls)
                {
                    files.Add(fl);
                }

                string[] subDirs = Directory.GetDirectories(dir);
                foreach (string subDir in subDirs)
                {
                    GetAllFiles(files, subDir);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
           
        }

        public void CleanDirectory(string dir)
        {
            foreach (string subdir in Directory.GetDirectories(dir))
            {
                Directory.Delete(subdir, true);
            }

            foreach (string subFile in Directory.GetFiles(dir))
            {
                File.Delete(subFile);
            }
        }

        public void CopyDirectory(string srcDir, string tgtDir)
        {
            DirectoryInfo source = new DirectoryInfo(srcDir);
            DirectoryInfo target = new DirectoryInfo(tgtDir);

            if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase))
            {
                throw new Exception("父目录不能拷贝到子目录！");
            }

            if (!source.Exists)
            {
                return;
            }

            if (!target.Exists)
            {
                target.Create();
            }

            FileInfo[] files = source.GetFiles();

            for (int i = 0; i < files.Length; i++)
            {
                File.Copy(files[i].FullName, Path.Combine(target.FullName, files[i].Name), true);
            }

            DirectoryInfo[] dirs = source.GetDirectories();

            for (int j = 0; j < dirs.Length; j++)
            {
                CopyDirectory(dirs[j].FullName, Path.Combine(target.FullName, dirs[j].Name));
            }
        }

        public void ReplaceExtensionName(string srcDir, string extensionName, string newExtensionName)
        {
            if (Directory.Exists(srcDir))
            {
                string[] fls = Directory.GetFiles(srcDir);

                foreach (string fl in fls)
                {
                    if (fl.EndsWith(extensionName))
                    {
                        File.Move(fl, fl.Substring(0, fl.IndexOf(extensionName)) + newExtensionName);
                        File.Delete(fl);
                    }
                }

                string[] subDirs = Directory.GetDirectories(srcDir);

                foreach (string subDir in subDirs)
                {
                    ReplaceExtensionName(subDir, extensionName, newExtensionName);
                }
            }
        }

        public bool CopyFile(string sourcePath, string targetPath, bool overwrite)
        {
            string sourceText = null;
            string targetText = null;

            if (File.Exists(sourcePath))
            {
                sourceText = File.ReadAllText(sourcePath);
            }

            if (File.Exists(targetPath))
            {
                targetText = File.ReadAllText(targetPath);
            }

            if (sourceText != targetText && File.Exists(sourcePath))
            {
                File.Copy(sourcePath, targetPath, overwrite);
                return true;
            }

            return false;
        }

    
    }
}